import { container } from "tsyringe";
import { Config } from "../config";
import { Sender } from "../sender";
import { WS } from "../ws";
import { Task } from "../task";
import { httpGet, httpPost } from "../http";
import { parseJson, configTokenFactory } from "../../utils/index";
import { CALL_WEB, CLOUD_WS, CONFIG, SENDER, TASK, HTTP_GET, HTTP_POST, DEVICE_NAME, DEVICE_UUID, STATUS_BAR_COLOR, TOKEN, CURRENT_ACTIVITY, CURRENT_PACKAGE, SHOW_CONSOLE, PUSH_TOKEN, APP_TITLE, TOOLBAR_BG_COLOR, LISTENERS, CURRENT_TASK, DEVICE_SETTING, LISTENER_ALL, SCRIPT_MANAGER, FLOATY_WINDOW } from "../../tokens/index";
import { commonRegister } from "./commonRegister";
import { ScriptManager } from "../scriptManager";
import { FloatyWindow } from "../floatWindow";

export function mainRegister() {
    commonRegister()
    container.registerSingleton(CONFIG, Config);
    container.registerSingleton(SENDER, Sender);
    container.registerSingleton(TASK, Task);
    container.registerSingleton(CLOUD_WS, WS);
    container.registerSingleton(SCRIPT_MANAGER, ScriptManager)
    container.registerSingleton(FLOATY_WINDOW, FloatyWindow)
    container.register(HTTP_GET, {
        useValue: httpGet
    })
    container.register(HTTP_POST, {
        useValue: httpPost
    })
    configTokenFactory(STATUS_BAR_COLOR)
    configTokenFactory(DEVICE_NAME)
    configTokenFactory(TOKEN)
    configTokenFactory(CURRENT_ACTIVITY)
    configTokenFactory(CURRENT_PACKAGE)
    configTokenFactory(SHOW_CONSOLE)
    configTokenFactory(PUSH_TOKEN)
    configTokenFactory(APP_TITLE)
    configTokenFactory(TOOLBAR_BG_COLOR)
    configTokenFactory(CURRENT_TASK)
    configTokenFactory(DEVICE_SETTING)
    configTokenFactory(DEVICE_UUID)

    // 注册调用webview函数
    container.register(CALL_WEB, {
        useFactory: () => {
            return (name: string, data: any) => {
                return new Promise((resolve, reject) => {
                    const main: any = ui.findView('main')
                    main.jsBridge.callHandler(name, data, (data: string) => {
                        const val = parseJson(data)
                        resolve(val)
                    })
                })
            }
        }
    })
}